package controller;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import net.GoogleResultItem;
import net.PageFetcher;
import code.extract.CodeJudge;
import code.extract.CodePageDealer;
import code.extract.HTMLEntityTransform;
import code.extract.SegmentItem;

public class GetCodeSegmentThread implements Runnable {
	public GoogleResultItem item;
	public int id;
	public String query;

	public GetCodeSegmentThread(String query, GoogleResultItem item, int id) {
		this.item = item;
		this.id = id;
		this.query = query;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		String content = PageFetcher.fetchURL(item.getOriginalURL());
		if (content != null && content.length() > 0) {
			extractCodeSegments(content, item.getOriginalURL(), id);
		}
	}

	public void extractCodeSegments(String pageContent, String originalURL,
			int id) {
		
//		pageContent = ESCHandler.replaceESC(pageContent);	//这里暂时还不能转义，因为内容中可能存在<>标签，此时会影响正则表达式的解析
		pageContent = CodePageDealer.eraseAllComment(pageContent);
		pageContent = CodePageDealer.eraseOneTagAndContent(pageContent, "head");
		pageContent = CodePageDealer.eraseOneTagAndContent(pageContent,
				"script");
		pageContent = CodePageDealer
				.eraseOneTagAndContent(pageContent, "style");
		pageContent.replaceAll("<br((\\s*?)|(\\s+?.*?))>", "\n");
		ArrayList<String> tagList = new ArrayList<String>();
		tagList.add("code");
		tagList.add("pre");
		tagList.add("div");
		tagList.add("p");
		tagList.add("td");
		ArrayList<SegmentItem> tList = CodePageDealer.getTextFragmentInPage(
				pageContent, tagList);
		tList = HTMLEntityTransform.segmentItemBatchTransform(tList, null);//这里对HTML的Entity进行转义
		tList = CodePageDealer.filterEmptySegmentItem(tList, null);
		int count = 0;
		PrintWriter pw;
		try {
			pw = new PrintWriter(new FileWriter(new File("./test_data/"+query+"/cs_"
					+ id + ".txt")));

			pw.println(originalURL);

			for (int i = 0; i < tList.size(); ++i) {
				if (!CodeJudge.isCode(tList.get(i).getContent())) {
					continue;
				}

				pw.println(count + "+++++++++++++++++++++++++++++++++++++++");
				if ((i - 1) >= 0) {
					pw.println("Text above:-----------------------------");
					pw.println(tList.get(i - 1).getContent());
				}
				pw.println("Code:-----------------------------");
				pw.println(tList.get(i).getContent());

				pw.println("Text below:-----------------------------");
				if ((i + 1) < tList.size()) {
					pw.println(tList.get(i + 1).getContent());
				}
			}
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
